package com.eastfair.projectcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.auth.api.UserAccountServiceFeign;
import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.auth.dto.UserAccountDto;
import com.eastfair.auth.vo.UserAccountVo;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.log.point.PointUtil;
import com.eastfair.point.pointlog.LogPoint;
import com.eastfair.projectcore.dto.*;
import com.eastfair.projectcore.entity.ProjectDb;
import com.eastfair.projectcore.service.ProjectDbService;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.projectcore.vo.ProjectDbVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * 项目分库分表信息
 * </p>
 *
 * @author ligang
 * @date 2021-07-07
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/projectDb")
@Api(value = "ProjectDb", tags = "项目分库分表信息")
public class ProjectDbController extends SuperController<ProjectDbService, Long, ProjectDb, ProjectDbPageQuery, ProjectDbSaveDTO, ProjectDbUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    ProjectDbService projectDbService;

    @Autowired
    PointUtil pointUtil;


    @Override
    public void handlerResult(IPage<ProjectDb> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ProjectDb> projectDbList = list.stream().map((map) -> {
            ProjectDb projectDb = ProjectDb.builder().build();
            //TODO 请在这里完成转换
            return projectDb;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(projectDbList));
    }

    /**
     * 获取雪花ID
     *
     * @param projectId 项目id
     * @return
     */
    @GetMapping("getSelfUid")
    public Long getSelfUid(@RequestParam(value = "projectId") Long projectId) {
        return projectDbService.getSelfUid(projectId);
    }


    /**
     * 批量获取雪花ID
     *
     * @param projectId 项目id
     * @param count     数量
     * @return
     */
    @GetMapping("batGetSelfUid")
    public List<Long> batGetSelfUid(@RequestParam(value = "projectId") Long projectId, @RequestParam(value = "count") Integer count) {
        return projectDbService.batGetSelfUid(projectId, count);
    }


    /**
     * 查询单个项目
     *
     * @param projectId 项目id
     * @return
     */
    @GetMapping("getById")
    public R<ProjectDb> getId(@RequestParam(value = "projectId") Long projectId) {
        return R.success(projectDbService.getByIdCache(projectId));
    }


    /**
     * 创建项目
     *
     * @param projectDbDTO 项目实体
     * @return
     */
    @ApiOperation(value = "创建项目", notes = "创建项目")
    @PostMapping("/create")
    public R create(@RequestBody ProjectDbDTO projectDbDTO) {
        return projectDbService.createProject(projectDbDTO);
    }

    /**
     * 创建管理项目
     *
     * @param projectDbDTO 项目实体
     * @return
     */
    @ApiOperation(value = "创建管理项目", notes = "创建管理项目")
    @PostMapping("/createAdminProject")
    public R createAdminProject(@RequestBody ProjectDbDTO projectDbDTO) {
        return projectDbService.createAdminProject(projectDbDTO);
    }



    /**
     * 更新管理项目
     *
     * @param projectDbDTO 项目实体
     * @return
     */
    @ApiOperation(value = "更新管理项目", notes = "创建管理项目")
    @PostMapping("/updAdminProject")
    public R updAdminProject(@RequestBody ProjectDbDTO projectDbDTO) {
        log.info("tarce is "+ContextUtil.getTrace());

        log.info("userid is "+ContextUtil.getUserId());

        log.info("ipaddr is "+ContextUtil.getIpAddr());

        projectDbDTO = new ProjectDbDTO();
        projectDbDTO.setId(1798309060716269568L);

        LogPoint logPoint = new LogPoint();
        logPoint.setEmail("数据步骤1");
        logPoint.setMsg("操作记录1");
        pointUtil.addLogPointMsg(logPoint);


//        return projectDbService.updAdminProject(projectDbDTO);
        return R.success();
    }



    /**
     * 修改项目
     *
     * @param projectDbDTO
     * @return
     */
    @ApiOperation(value = "修改项目", notes = "修改项目")
    @PutMapping("/update")
    public R<Boolean> update(@RequestBody ProjectDbDTO projectDbDTO) {
        return baseService.updateProject(projectDbDTO);
    }

    /**
     * 查询单个项目
     *
     * @param id 主键id
     * @return
     */
    @GetMapping("/getAndRoleById/{id}")
    public R<ProjectDbVo> getAndRoleById(@PathVariable(value = "id") Long id) {
        return R.success(projectDbService.getAndRoleById(id));
    }


    /**
     * 查询项目的展会集合
     *
     * @return
     */
    @GetMapping("/queryExhibition")
    public R<List<ProjectDbVo>> queryExhibition(Boolean isAllProject) {
        return R.success(Arrays.asList(projectDbService.queryExhibition(ContextUtil.getProjectId())));
    }





    /**
     * 分布式事务测试
     *
     * @param projectDbDTO 项目实体
     * @return
     */
    @ApiOperation(value = "分布式事务测试", notes = "分布式事务测试")
    @PostMapping("/trasactionTest")
    public R trasactionTest(@RequestBody ProjectDbDTO projectDbDTO) {
        return projectDbService.trasactionTest(projectDbDTO);
    }
}
